package oas.web;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;


/**
 * webMVC配置
 *
 * @author liyue
 * @version v1.0.0
 */
@Configuration
@EnableWebMvc
@ComponentScan("oas.web")
public class WebConfig extends WebMvcConfigurerAdapter {

	/**
	 * 自定义视图解析器
	 *
	 * @return
	 */
	@Bean
	public ViewResolver viewResolver() {
		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
		resolver.setPrefix("/WEB-INF/views/");
		resolver.setSuffix(".jsp");
		return resolver;
	}

	/**
	 * 注册一个默认的Handler：DefaultServletHttpRequestHandler，这个Handler也是用来处理静态文件的，
	 *
	 */
	@Override
	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}

	/**
	 * 定义静态资源处理
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
		super.addResourceHandlers(registry);
	}

	/**
	 * 定义拦截器
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new ManagerInterceptor())
				.addPathPatterns(new String[] {"/user/**", "/workcells/**", "/manager/**","/employee", "/employee/**"
				})// 添加拦截
				.excludePathPatterns("");// excludePathPatterns 排除拦截
		registry.addInterceptor(new UserInterceptor())
				.addPathPatterns(new String[] { "/user/**", "/attendances", "/leave" })// 添加拦截
				.excludePathPatterns("");// excludePathPatterns 排除拦截
		super.addInterceptors(registry);

	}

}